package com.itheima.service.impl;

import com.itheima.dao.QuestionDao;
import com.itheima.domain.Question;
import com.itheima.domain.User;
import com.itheima.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
//
  @Autowired
    private QuestionDao questionDao;

    public void save(Question question, HttpSession session) {
        System.out.println("question2 = " + question);

        question.setCreatetime(new Date());
        //注意登录后才能取session
        User user = (User) session.getAttribute("user");
        question.setUid(user.getUid());
        question.setIsUp("0");
        System.out.println("question3 = " + question);

        questionDao.save(question);
    }

    public Boolean update(Question question) {
        return questionDao.update(question)>0;
    }

    public Boolean delete(Integer qid) {
        return questionDao.delete(qid)>0;
    }

    public Question getById(Integer qid) {
        Question question = questionDao.getById(qid);
        return question;
    }

    public List<Question> getAll() {
        List<Question> questions = questionDao.getAll();
        return questions;
    }

    @Override
    public Integer getTotalPage(Integer pageCount, String title) {
        if(title == null){
            title = "";
        }
        int totalCount = questionDao.selectTotalCount(title);//总记录数

        return (totalCount - 1) / pageCount + 1;
    }

    @Override
    public List<Question> getAllByPage(Integer currentPage, Integer pageCount, String title) {

        return questionDao.getByPage((currentPage-1)*pageCount,pageCount,title);
    }

    @Override
    public List<Question> getAllQues(String title) {
        List<Question> questions = questionDao.getAllQues(title);
        return questions;
    }

    @Override
    public Boolean updateStatus(int is_up, int qid) {
        int i = questionDao.updateStatus(is_up, qid);
        return i > 0;
    }

    @Override
    public List<Question> getAllUp() {
        List<Question> questions = questionDao.getAllUp();
        return questions;
    }

    @Override
    public List<Question> getByUid(Integer uid) {
        List<Question> byUid = questionDao.getByUid(uid);
        return byUid;
    }
}
